package com.web.logindemo.config.manager;


import com.web.logindemo.common.util.Threads;
import com.web.logindemo.utils.spring.SpringUtils;

import javax.swing.*;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 异步任务管理器
 */
public class AsyncManager {
    /**
     * 操作延迟10ms
     */
    private final int OPERATE_DELAY_TIME = 10;

    /**
     * 异步操作任务调度池
     */
    private ScheduledExecutorService executor = SpringUtils.getBean("scheduledExecutorService");

    /**
     * 单例模式
     */
    private AsyncManager(){};

    private static AsyncManager me = new AsyncManager();

    public static  AsyncManager me(){
        return me;
    }

    /**
     * 执行任务
     */
    public void execute(TimerTask task){
        executor.schedule(task, OPERATE_DELAY_TIME, TimeUnit.MILLISECONDS);
    }

    /**
     * 停止任务池
     */
    public void shutdown(){
        Threads.shutdownAndAwaitTermination(executor);
    }
}
